package com.api.action.v1;

import com.admin.entity.SystemApk;
import com.admin.mapper.SystemApkMapper;
import com.aliyun.OssUpload;
import com.api.service.v1.SystemApkApiService;
import com.api.service.v1.WelcomeApiService;
import com.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/")
public class SystemApkApiAction {

    @Autowired
    private SystemApkApiService systemApkApiService;

    @Autowired
    private WelcomeApiService welcomeApiService;

    @Autowired
    private OssUpload ossUpload;


    @Value("${SERVER}")
    private String ECS_SERVER;

    @RequestMapping("/SystemAPKUpdate")
    public ResponseBean SystemAPKUpdate(String mac, String versionCode) {
        if (mac == null || mac.trim().length() == 0) {
            Map map = new HashMap();
            map.put("version", 1);
            map.put("url", "");
            return new ResponseBean(500, "参数有误", map);
        }
        SystemApk systemApk = systemApkApiService.getCurrentVersion(mac);
        if (systemApk==null){
            return new ResponseBean(500, "不要升级", null);
        }
        String status = systemApk.getSystemApkStatus();
        if (systemApk != null && "启用".equals(status)) {
            String version = systemApk.getSystemApkVesion();
            if (version != null && versionCode!=null && Float.parseFloat(version)>Float.parseFloat(versionCode)) {
                Map map = new HashMap();
                List servers = welcomeApiService.getServers(mac);
                String server = ECS_SERVER;


                String url = systemApk.getSystemApkUrl();
                if (url!=null && url.startsWith("/"))
                {
                    url = url.substring(1);
                }
                map.put("version", version);
                map.put("url", server + url+ "?md5=" + systemApk.getSystemApkMd5());
                map.put("servers", servers);
                return new ResponseBean(200, "success", map);
            }
        }
        return new ResponseBean(500, "不要升级", null);
    }
}
